package com.secfound.soccer.server;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.secfound.soccer.client.BettingService;
import com.secfound.soccer.server.persistence.DataPersistence;
import com.secfound.soccer.server.tools.ScoreValidator;
import com.secfound.soccer.shared.db.Bet;
import com.secfound.soccer.shared.db.Challenger;
import com.secfound.soccer.shared.db.Game;
import com.secfound.soccer.shared.db.Team;

/**
 * Handles listing of games, bets
 * ahd booking of a bet or training bets
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class BettingServiceImpl extends RemoteServiceServlet implements BettingService {
	
	DataPersistence dp = new DataPersistence();

	@Override
	public String checkScore(Bet bet) throws IllegalArgumentException {
		dp.setupDB();
		Game realGame = dp.queryGame(bet.getaTeam(), bet.getaScore(), bet.getbTeam(), bet.getbScore(), bet.getStartTime());		
		String output="Sorry That wasn't the score.";
		if(ScoreValidator.check(realGame, bet)){
			output="Score OK";
		}
		return output;
	}

	@Override
	public List<Game> listGames(Game game) throws IllegalArgumentException {
		List<Game> gameList =null;
		UserService userService = UserServiceFactory.getUserService();
		if(userService.isUserLoggedIn()){
			dp.setupDB();
			dp.closeAllBets();
			gameList = dp.listGames(game);
		}
		return gameList;
	}
	
	@Override
	public Challenger storeBet(Bet bet) throws IllegalArgumentException {
		Challenger chall = dp.loadChallenger( bet.getChallenger() );
		if(chall==null){
			chall=new Challenger();
			chall.setEmail(bet.getChallenger());
		}		
		Game realGame = dp.queryGame(bet.getaTeam(), bet.getaScore(), bet.getbTeam(), bet.getbScore(), bet.getStartTime());	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date currentTime = new Date();
		
		String strCurrentTime = sdf.format( currentTime );
		bet.setCreatedTime(strCurrentTime);
		bet.setCreated(currentTime);
		bet.setId( dp.getBetKey(bet) ) ;
		
		String output="";
		if(realGame != null ){
			if( realGame.getStartTime().compareTo(  strCurrentTime ) >= 0 ){
				output="Your bet has been placed. Good Luck!";
				dp.storeBet(bet);	
			}else{
				boolean isAnswerOk =ScoreValidator.check(realGame, bet);
				if(isAnswerOk){
					output="Congrats! Your score is Correct!";
				}else{
					output="Sorry, that was not the score. Try again!";
				}
				dp.storeTrainingBet(bet, isAnswerOk);
			}
		}
		chall.setMsg(output);
		return chall;
	}

	@Override
	public List<Bet> listBets(Bet bet) throws IllegalArgumentException {
		return dp.listBets(bet.getChallenger());
	}

	@Override
	public List<Team> listTeams(Team team) throws IllegalArgumentException {		
		return dp.listTeams(team);
	}
}
